// --------------------------------------------------------------------------------------------------------------------
// <copyright file="CloudStorageAccountComparer.cs" company="AzureContrib (Open source library http://azurecontrib.codeplex.com)">
//   Microsoft Public License (Ms-PL)
// </copyright>
// <summary>
//   Exception for errors when handling Windows Azure Cloud Drives.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;

using Microsoft.WindowsAzure;

namespace AzureContrib.WindowsAzure.StorageClient
{
	/// <summary>
	/// <see cref="IEqualityComparer{T}"/> for <see cref="CloudStorageAccount"/>
	/// </summary>
	public class CloudStorageAccountComparer : IEqualityComparer<CloudStorageAccount>
	{
		/// <summary>
		/// Uses an <see cref="IEqualityComparer{T}.Equals(T,T)"/> to compare the two accounts.
		/// </summary>
		/// <param name="x">First <see cref="CloudStorageAccount"/></param>
		/// <param name="y">Second <see cref="CloudStorageAccount"/></param>
		/// <returns>True if the <see cref="CloudStorageAccount"/> are equal.</returns>
		[SuppressMessage("Microsoft.Design", "CA1062:ValidateArgumentsOfPublicMethods", Justification = "ObjectEqualityHelpers.NotEquatable handles null checking.", Scope = "method")]
		public bool Equals(CloudStorageAccount x, CloudStorageAccount y)
		{
			bool result;
			if (ObjectEqualityHelpers.NotEquatable(x, y, out result))
			{
				return result;
			}

			// if (x.BlobEndpoint.AbsoluteUri.EndsWith("/devstoreaccount1"))
			return new StorageCredentialsComparer().Equals(x.Credentials, y.Credentials) &&
						 x.BlobEndpoint.AbsoluteUri == y.BlobEndpoint.AbsoluteUri &&
						 x.TableEndpoint.AbsoluteUri == y.TableEndpoint.AbsoluteUri &&
						 x.QueueEndpoint.AbsoluteUri == y.QueueEndpoint.AbsoluteUri;
		}

		/// <summary>
		/// Calls <see cref="IEqualityComparer{T}.GetHashCode(T)"/>
		/// </summary>
		/// <param name="obj">The <see cref="CloudStorageAccount"/> to calculate hash for.</param>
		/// <returns>The hash code for <see cref="CloudStorageAccount"/></returns>
		public int GetHashCode(CloudStorageAccount obj)
		{
			if (obj == null)
// ReSharper disable HeuristicUnreachableCode
			{
				return 0;
			}
// ReSharper restore HeuristicUnreachableCode
			return new StorageCredentialsComparer().GetHashCode(obj.Credentials) ^
						 obj.BlobEndpoint.AbsoluteUri.GetHashCode() ^
						 obj.TableEndpoint.AbsoluteUri.GetHashCode() ^
						 obj.QueueEndpoint.AbsoluteUri.GetHashCode();
		}
	}
}